﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Web;
using System.Web.SessionState;

namespace cebao.Web.Admin
{
    /// <summary>
    /// getinfo 的摘要说明
    /// </summary>
    public class getinfo : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            string ac = "";
            if (null != context.Request.QueryString["action"])
            {//获取前台传来的值  
                ac = context.Request.QueryString["action"].ToString().Trim();
            }
            if (ac == "level")
                QueryLevel(context);
            else if (ac == "loginout")
                loginout(context);
            else if (ac == "loginname")
                loginname(context);
            else if (ac == "myzu")
                myzu(context);//得到用户所在的组
            else if (ac == "forest")//得到某一害相关ID的上传数据
                QueryForest(context);

        }
        public void myzu(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string name = "";
            int id = getID(context);
            if (id != 0)
            {//获取前台传来的值 
                cebao.BLL.userinfo buser = new BLL.userinfo();
                name = buser.GetZu(id);
            }
            context.Response.Write(name);
            context.Response.End();
        }
        public void loginname(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string name = "";
            bool isok = false;
            if (null != context.Request.Form["name"])
            {//获取前台传来的值  
                name = context.Request.Form["name"].ToString().Trim();
                cebao.BLL.userinfo buser = new BLL.userinfo();
                isok = buser.Exists(name);
            }
            context.Response.Write(isok);
            context.Response.End();
        }
        public void loginout(HttpContext context)
        {
            context.Response.ContentType = "text/plain";

            context.Response.Write("");
            context.Response.End();
        }
        public int getID(HttpContext context)
        {
            int id = 0;
            if (context.Session["user"] != null)
            {
                DataTable dt = context.Session["user"] as DataTable;
                if (dt != null)
                {
                    id = int.Parse(dt.Rows[0]["uid"].ToString());
                }
            }
            return id;
        }
        public int getLevel(HttpContext context)
        {
            int id = 0;
            if (context.Session["user"] != null)
            {
                DataTable dt = context.Session["user"] as DataTable;
                if (dt != null)
                {
                    id = int.Parse(dt.Rows[0]["ulevel"].ToString());
                }
            }
            return id;
        }
        public string getUname(HttpContext context)
        {
            string name = "";
            if (context.Session["user"] != null)
            {
                DataTable dt = context.Session["user"] as DataTable;
                if (dt != null)
                {
                    name = dt.Rows[0]["uname"].ToString();
                }
            }
            return name;
        }
        public string getUadd(HttpContext context)
        {
            string name = "";
            if (context.Session["user"] != null)
            {
                DataTable dt = context.Session["user"] as DataTable;
                if (dt != null)
                {
                    name = dt.Rows[0]["uadd"].ToString();
                }
            }
            return name;
        }
        public string getPCC(HttpContext context)
        {
            string pcc = ",,";
            if (context.Session["user"] != null)
            {
                DataTable dt = context.Session["user"] as DataTable;
                if (dt != null)
                {
                    pcc = dt.Rows[0]["uprovince"].ToString() + "," + dt.Rows[0]["ucity"].ToString() + "," + dt.Rows[0]["ucounty"].ToString();
                }
            }
            return pcc;
        }
        public void QueryLevel(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int level = 0;
            string[] pcc;
            string name = "", uadd = "";
            level = getLevel(context);
            pcc = getPCC(context).Split(',');
            name = getUname(context);
            uadd = getUadd(context);
            string json = "[{\"level\":\"" + level + "\",\"province\":\"" + pcc[0] + "\",\"city\":\"" + pcc[1] + "\",\"county\":\"" + pcc[2] + "\",\"uadd\":\"" + uadd + "\",\"name\":\"" + name + "\"}]";
            context.Response.Write(json);
            context.Response.End();
        }
        public void QueryForest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            if (null != context.Request.QueryString["pid"] && null != context.Request.QueryString["atype"])
            {
                //string pidstr = context.Request.QueryString["pid"].ToString();
                int pid = 0;
                int.TryParse(context.Request.QueryString["pid"].ToString(), out pid);
                string pets = "", stage = "", stati = "", num = "", address="",inputtime="",json = "";
                if (pid != 0)
                {
                    string atype = context.Request.QueryString["atype"].ToString();
                    if (atype == "pet")
                    {
                        cebao.BLL.ForestPets b = new BLL.ForestPets();
                        cebao.Model.ForestPets model = new Model.ForestPets();
                        model = b.GetModel(pid);
                        pets = model.petsName;
                        stage = model.petStage;
                        stati = model.totalUnit;
                        num = model.totalCount;
                        address = model.address;
                        inputtime = model.inputTime.Value.ToString();
                    }
                    else if (atype == "rat")
                    {
                        cebao.BLL.ForestRat b = new BLL.ForestRat();
                        cebao.Model.ForestRat model = new Model.ForestRat();
                        model = b.GetModel(pid);
                        pets = model.ratName;
                        stage = "无";
                        stati = model.totalUnit;
                        num = model.totalCount;
                        address = model.address;
                        inputtime = model.inputTime.Value.ToString();
                    }
                    else if (atype == "sickness")
                    {
                        cebao.BLL.ForestSickness b = new BLL.ForestSickness();
                        cebao.Model.ForestSickness model = new Model.ForestSickness();
                        model = b.GetModel(pid);
                        pets = model.sicknessName;
                        stage = "无";
                        stati = model.totalUnit;
                        num = model.totalCount;
                        address = model.address;
                        inputtime = model.inputTime.Value.ToString();
                    }
                    else if (atype == "grass")
                    {
                        cebao.BLL.ForsetGrass b = new BLL.ForsetGrass();
                        cebao.Model.ForsetGrass model = new Model.ForsetGrass();
                        model = b.GetModel(pid);
                        pets = model.grassName;
                        stage = "无";
                        stati = model.totalUnit;
                        num = model.totalCount;
                        address = model.address;
                        inputtime = model.inputTime.Value.ToString();
                    }
                    json = "[{\"pet\":\"" + pets + "\",\"stage\":\"" + stage + "\",\"stati\":\"" + stati + "\",\"num\":\"" + num + "\",\"address\":\"" + address + "\",\"inputtime\":\"" + inputtime + "\"}]";
                }
                context.Response.Write(json);
            }
            context.Response.End();
        }


        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}